<template>
  <div>
    <div class="margin-bottom">
      <el-input
        clearable
        placeholder="输入权限字符串搜索"
        size="mini"
        style="width: 200px;"
        class="filter-item"
      />
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-search">搜索</el-button>
      <el-button style="float:right" type="primary" size="mini" icon="el-icon-plus" @click="add()">添加</el-button>
    </div>
    <el-table v-loading="loading" :data="permissions" border style="width: 100%">
      <el-table-column prop="permission" label="权限字符串"/>
      <el-table-column prop="permissionName" label="描述"/>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini" @click="edit(scope.row)"/>
          <el-popover :ref="scope.row.id" placement="top" width="180">
            <p>确定删除本条数据吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
              <el-button
                :loading="delLoading"
                type="primary"
                size="mini"
                @click="remove(scope.row)"
              >确定</el-button>
            </div>
            <el-button slot="reference" type="danger" size="mini" icon="el-icon-delete"/>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="margin-top"
      background
      layout="prev, pager, next"
      :page-count="pageConfig.total"
      @current-change="currentChange"
    />
    <el-dialog :close-on-click-modal="false" :title="!isEdit?'新建权限':'编辑权限'" :visible.sync="isShow" width="600px">
      <el-form ref="form" label-width="100px" :model="form" :rules="rules">
        <el-form-item label="权限字符串" prop="permission">
          <div class="text-input">
            <el-input
              v-model="form.permission"
              auto-complete="off"
              maxlength="20"
              class="el-input-width"
            />
          </div>
        </el-form-item>
        <el-form-item label="权限描述" prop="permissionName">
          <div class="text-input">
            <el-input
              v-model="form.permissionName"
              auto-complete="off"
              maxlength="20"
              class="el-input-width"
            />
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="isShow = false">取 消</el-button>
        <el-button type="primary" @click="confirm()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  permission_add,
  permission_update,
  permission_remove,
  permission_page
} from '@/api/system';
export default {
  data() {
    return {
      permissions: [],
      loading: false,
      isShow: false,
      isEdit: true,
      delLoading: false,
      pageConfig: {
        page: 0,
        pageSize: 10,
        total: 0
      },
      form: {
        id: null,
        permission: '',
        permissionName: ''
      },
      rules: {
        permission: [
          { required: true, message: '请输入权限字符串', trigger: 'blur' },
          { min: 2, max: 20, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ],
        permissionName: [
          { required: true, message: '请输入权限描述', trigger: 'blur' },
          { min: 2, max: 12, message: '长度在 2 到 20 个字符', trigger: 'blur' }
        ]
      }
    };
  },
  mounted() {
    this.getPermissions();
  },
  methods: {
    async getPermissions() {
      this.loading = true;
      const res = await permission_page({
        page: this.pageConfig.page,
        pageSize: this.pageConfig.pageSize
      });
      this.permissions = res.data.pageData;
      this.pageConfig.total = res.data.total;
      this.loading = false;
    },
    currentChange(value) {
      this.pageConfig.page = value - 1;
      this.getPermissions();
    },
    add() {
      this.isEdit = false;
      this.isShow = true;
      this.form = {};
    },
    edit(permission) {
      this.isEdit = true;
      this.isShow = true;
      this.form = JSON.parse(JSON.stringify(permission));
    },
    async remove(permission) {
      this.$confirm('是否删除该权限?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await permission_remove(permission.id, {
          id: permission.id
        });
        this.$message({
          type: 'success',
          message: '删除成功!'
        });
      });
    },
    confirm() {
      this.$refs['form'].validate(async valid => {
        if (valid) {
          if (this.isEdit) {
            await permission_add(this.form);
          } else {
            await permission_update(this.form);
          }
          this.isShow = false;
          this.getPermissions();
        }
      });
    }
  }
};
</script>
